import java.awt.Point;

import org.jhotdraw.framework.DrawingEditor;
import org.jhotdraw.framework.DrawingView;

public class ProxyDiagram
{
	/*
	 * MEMBER VARIABLES
	 */
	
	private DrawableDiagram _activeDiagram;
	private DrawingView view;
	private int figureCount;
	private StateFigure[] _stateFigures = new StateFigure[10];
	private MainWindow _mw;
	
	
	
	/*
	 * CONSTRUCTORS
	 */
	
	public ProxyDiagram(DrawableDiagram diagram)
	{
		this._activeDiagram = diagram;
	}
	
	public ProxyDiagram()
	{
		this._activeDiagram = null;
	}
	
	
	
	/*
	 * CONCRETE METHODS
	 */
	
	public String writeSave()
	{
		return this._activeDiagram.writeSave();
	}
	
	// Increases the size of this._stateFigures
	public void increaseStateFigure()
	{
		StateFigure[] newArray = new StateFigure[this._stateFigures.length + 10];
		for (int i = 0; i < this._stateFigures.length; i++)
		{
			newArray[i] = this._stateFigures[i];
		}
		this._stateFigures = newArray;
	}
	
	// Tells _activeDiagram to draw itself.  
	public void draw()
	{
		this._activeDiagram.draw();
	}
	
	public void drawState(State state)
	{
		int[] coords = state.getCoords();
		StateFigure s1;
		if (!(coords[0] < 0 || coords[1] < 0))
		{
			s1 = new StateFigure(coords[0], coords[1], state.getName());
		}
		else
		{
			s1 = new StateFigure(this.getPoint().x, this.getPoint().y, state.getName());
		}
		State[] s1sa = ((FiniteAutomata)this._activeDiagram).getStates();
		if (s1sa[0].getName().equals(state.getName()))
		{
			s1.setIsStartState(true);
		}
		s1.setIsAcceptState(state.getAccepting());
		this.view.add(s1);
		if (this.figureCount == this._stateFigures.length)
		{
			this.increaseStateFigure();
		}
		this._stateFigures[this.figureCount]= s1;
		this.figureCount++;
	}
	
	public void drawTransition(int s1, int s2, String letter)
	{
		Transitions t1 = new Transitions(this._stateFigures[s1 - 1], this._stateFigures[s2 - 1], letter, this.view);
		this.view.add(t1);
	}
	
	public String isValid()
	{
		return this._activeDiagram.isValid();
	}
	
	public boolean simulate(String[] word)
	{
		return this._activeDiagram.simulate(word);
	}
	
	// Places states in a grid with 3 columns
	public Point getPoint()
	{
		int y = 60 + 175 * (int)Math.floor(this.figureCount / 3);
		int x = 50 + 175 * (int)(this.figureCount % 3);
		return new Point(x, y);
	}
	
	
	
	/*
	 * GETTERS AND SETTERS
	 */
	
	public void setMainWindow(MainWindow mw)
	{
		this._mw = mw;
		this.view = this._mw.view();
	}
	
	public void setActiveDiagram(DrawableDiagram dd)
	{
		this._activeDiagram = dd;
	}
}